Repository navigation
Analytics: explain token use by part, kind of work and model; an honest tokens per merged PR - #662
Merged
Merged
Conversation
…r-merged-PR that counts only the runs behind merged PRs The tile divided every run's tokens by the merged pull requests and compared periods whose run records were written differently (before #346 a resume or CI fix SET a run's tokens, dropping earlier invocations). - common::usage: UsageSlice, slices() over the per-feature and per-model counters every call already writes, total_slice(), price_known(), and work_kind (each feature in exactly one kind of work). - GET /api/analytics gains token_use: the period's total by part with cost, by day, by kind of work and by model (cache hit share, whether the price is known) from the meter's DAY# items; tokens per merged PR from the runs started in the period whose pull request merged, also by kind of run; run tokens by how the work ended; the heaviest runs and pull requests. - A number is compared with the period before only when both were recorded the same way: the metered total when per-call counting covers the period before, run-record numbers when it starts on or after the day run records became complete. Otherwise previous is null and the reason is named. - GET /api/usage gains models (tokens by part and cache hit rate per model); docs/api/openapi.json regenerated. No new storage: the breakdown counters already exist per day, and nothing before per-call counting can be split by kind or model.
Analytics shows tokens, not money, so token_use carries no cost: the cost fields, the per-PR cost, the previous cost and price_known are gone, with common::usage::price_known, which nothing else used. A test asserts the answer names no cost or price.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The Analytics tile "Tokens per merged PR" could not be explained from the page, and its change against the period before was not a like-for-like comparison. This adds a
token_usesection toGET /api/analyticsthat says what a period's tokens were (new input, output, cache read, cache write), what kind of work and which model used them, what a merged pull request took, and which runs and pull requests were heaviest. A number is compared with the period before only when both were recorded the same way.How the tile was computed (unchanged fields, kept for older dashboards):
summary()inservices/gateway/src/routes/analytics.rssummed all four token parts of every run started in the period (failed runs, runs whose pull request is still open or closed, runs with no pull request) and divided by the distinct merged pull requests among those runs. Reviews, chat and log analysis were never in it (they are not runs). Fewer merges alone raised it.Was the jump a change in counting? Partly, and the code cannot say how much:
counts_cacheonly changes what the monthly token limit counts (common::token_limits). It never touched this tile.DAY#items with per-feature and per-model counters) exists from fix: count every model call once, with all four token parts and per-model prices #166 (2026-10-18). Nothing before it can be split by kind of work or model.Changes
common::usage:UsageSlice(four parts, calls, cost),slices()to read the per-feature and per-model counters every call already writes,total_slice(), andwork_kind(each feature in exactly one kind of work).gateway::routes::analytics::token_use(new module), returned astoken_use:total,daily,by_kind,by_modelfrom the meter'sDAY#items (every model call, on the day it was made). With one repository picked, or nothing metered, they come from the run records andbasisisruns(no models).run,plan), security (security_fix,security), follow-ups (feedback: review comments, CI fixes and conflicts together, the meter does not tell them apart), reviews, chat (chat,brain,agent_ask), logs (log_analysis,alert), other. Each row lists the features lumped into it.by_model: parts, calls andcache_hit_share(cache read out of all input).per_merged_pr: runs started in the period whose pull request merged, every run of the period on that pull request, and nothing else; also by kind of run (ticket, security fix, alert).runs.by_outcome: every run of the period in exactly one of merged, open, closed, failed, no pull request.heaviest_runs,heaviest_prs(all runs on one pull request added up), with run ids and pull request links.comparable/not_comparable/previous: the metered total is compared only when per-call counting covers the whole period before (first metered day read with one query); run-record numbers only when the period before starts on or afterRUN_RECORDS_COMPLETE_FROM(2026-10-28), or after the install's own first whole metered day when that is later. Otherwisepreviousis null and the reason is named.GET /api/usage(public):models, tokens by part and cache hit rate per model for the month. Tokens only, like the rest of that body.docs/api/openapi.jsonregenerated.Storage: no new table, item or infrastructure. The per-feature and per-model counters are the durable aggregates, already updated with every recorded call; the page reads up to one
DAY#item per day. The heaviest lists and per-merged-PR numbers use the run rows the route already loads for the period. No backfill: before the meter existed there is no per-call record to rebuild a split by kind or model from (run records keep no model, and pass traces expire after 30 days), and writing estimates intoDAY#items would break what the daily check against the provider's report compares.Notes for review:
token_useis tokens only: no cost fields, in line with the Analytics page showing tokens only. A test asserts the answer names no cost, price or dollar amount.RUN_RECORDS_COMPLETE_FROMis the release day after Run pipeline: cost only added, security findings kept, early failures said, multi-repo PRs paired #346. A self-hosted install that upgraded later is covered by its first metered day only when the same upgrade also brought fix: count every model call once, with all four token parts and per-model prices #166.Verification
cargo fmt --check --all,cargo clippy --workspace --all-targets -- -D warnings,cargo test --workspace: pass (1754 worker tests, the gateway and common suites, 0 failed).